class Admin::OptionTypesController < AdminController
  before_action :set_shop_category

  def index
    @option_types = @shop_category.option_types.order("created_at DESC")
  end

  def new
    @option_type = OptionType.new
  end

  def create
    @option_type = @shop_category.option_types.build option_type_params
    if @option_type.save
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "保存成功！"
    else
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "保存失败！"
    end
  end

  def edit
    @option_type = OptionType.find params[:id]
  end

  def update
    @option_type = OptionType.find params[:id]
    if @option_type.update option_type_params
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "更新成功！"
    else
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "更新失败！"
    end
  end
  
  def destroy
    @option_type = OptionType.find params[:id]
    if @option_type.destroy
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "删除成功."
    else
      redirect_to admin_shop_category_option_types_path(@shop_category), notice: "删除失败."
    end
  end

  private

  def set_shop_category
    @shop_category = ShopCategory.find params[:shop_category_id]
  end

  def option_type_params
    params.require(:option_type).permit(:name, :shop_category_id)
  end
end
